package sliding_window;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-12-04
 * Time: 11:18
 */
public class MinSubArrayLen {
    public static int minSubArrayLen(int target, int[] nums) {
        int len = Integer.MAX_VALUE;
        int sum = 0;
        for(int right = 0, left = 0; right < nums.length; right++) {
            sum += nums[right];//进窗口
            while (sum >= target) {
                len = Math.min(len, right - left + 1);//更新结果
                sum -= nums[left++];//出窗口
            }
        }
        return len == Integer.MAX_VALUE ? 0 : len;
    }

    public static void main(String[] args) {
        System.out.println(minSubArrayLen(15, new int[]{1,2,3,4,5}));
    }
}
